using System;
using UnityEngine;

[Serializable]
public class TargetParameter
{
	public int maxValue = 1;

	public MissionTargetBase target;

	public string nameIcon = string.Empty;

	public string msgPurpose = string.Empty;

	public bool autoSaveValue;

	public int curValue;

	public bool showIndicator = true;

	[HideInInspector]
	public bool isStarted;

	[HideInInspector]
	public bool isComplete;

	[HideInInspector]
	public MissionBase curMission;

	[HideInInspector]
	public static string keyCompleted = "_Compl";

	public void StartTarget()
	{
		UpdateIsCompletedTarget();
		if (!isComplete)
		{
			if (BCWDebug.isDebugBuild)
			{
				BCWDebug.Log("StartTarget = " + target.gameObject.name);
			}
			if (autoSaveValue)
			{
				LoadValue();
			}
			target.curParametr = this;
			target.curMission = curMission;
			target.needCountTarget = maxValue;
			target.curCountTarget = curValue;
			if (!isStarted)
			{
				target.OnStart();
			}
		}
	}

	public void LoadValue()
	{
		if (curMission != null)
		{
			curValue = BCWStorage.Get<int>(curMission.gameObject.name + "_" + target.gameObject.name);
		}
	}

	public void SaveValue()
	{
		if (curMission != null && autoSaveValue)
		{
			BCWStorage.Set(curMission.gameObject.name + "_" + target.gameObject.name, curValue);
		}
	}

	public void UpdateValue()
	{
		curValue = target.curCountTarget;
		SaveValue();
		ManagerMissions.OnUpdateTargetValue();
	}

	public void OnStopTarget()
	{
		target.OnStopTarget();
	}

	public void ResetValue()
	{
		curValue = 0;
		SaveValue();
		SetIsCompletedTarget(false);
	}

	public void SetIsCompletedTarget(bool val)
	{
		if (curMission != null)
		{
			if (BCWDebug.isDebugBuild)
			{
				BCWDebug.Log("CompletedTarget = " + target.gameObject.name);
			}
			isComplete = val;
			if (val && (bool)target.gameObject.GetComponent<MissionTargetCheckpoint>())
			{
				AllUnitaryAchievements.instance.finishQuests_5_WithChepoints.Increment();
			}
			string key = curMission.gameObject.name + "_" + target.gameObject.name + keyCompleted;
			BCWStorage.Set(key, val);
			if (isComplete)
			{
				ManagerMissions.OnCompleteTarget();
			}
		}
	}

	public void UpdateIsCompletedTarget()
	{
		if (curMission != null)
		{
			string key = curMission.gameObject.name + "_" + target.gameObject.name + keyCompleted;
			isComplete = BCWStorage.Get<bool>(key);
		}
	}
}
